<?php
/**
 * 账号管理相关操作
 * 2016-9-7 15:51:27
 * @version 1.0
 */
namespace Home\Controller;
class AccountmanageController extends PmpController
{
    /** 
     * 首页
     * 2016-9-7 15:55:39
     * @version 1.0
     */
    public function index()
    {       
        $user_name = $_REQUEST['user_name'];
        $memberUserModel = D('MemberUser');
        $userArray = $memberUserModel->getUserInfo($user_name);
        $page = new \Think\Page(count($userArray),C('PAGE_LINE'));
        //设定显示页数
        $show = $page->show();//分页显示输出
        $userArray = $memberUserModel->getUserInfo($user_name,$page);
        
        foreach ($userArray as $key => $value)
        {
            if ($value['status'] == '1')
            {
                $userArray[$key]['status_img'] = C('URL_POST') . 'Public/img/icons/open_btn.png';
                $userArray[$key]['status_name'] = '启用';
            }
            else
            {
                $userArray[$key]['status_img'] = C('URL_POST') . 'Public/img/icons/close_btn.png';
                $userArray[$key]['status_name'] = '停用';
            }
            if ($value['avatar'] == null)
            {
                $userArray[$key]['avatar'] = C('URL_POST') . 'Public/img/icons/un_header.png';
            }
            else
            {
                $userArray[$key]['avatar'] = C('URL_ADMIN_POST') . $value['avatar'];
            }
        }
        $this->assign("userArray",$userArray);
        $this->assign("user_name",$user_name);
        $this->assign("page",$show);
        $this->display();
    }
    
    /**
     * 添加用户
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function add()
    {
        // 获取用户类型
        $groupArray = D('GroupInfo')->getGroupInfo();
        $this->assign('groupArray', $groupArray);
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-14 14:21:46
     * sz
     */
    public function submit_add ()
    {
        $user_name = $_POST['user_name'];
        $real_name = $_POST['real_name'];
        $password = $_POST['password'];
        $group_id = $_POST['group'];
        $cellphone = $_POST['cellphone'];
      
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array(
                'jpg',
                'gif',
                'png',
                'jpeg'
        ); // 设置附件上传类型
        $upload->rootPath = C('URL_ADMIN');// 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->uploadOne($_FILES['logo_head']);
        $avatar = $info['savepath'] . $info['savename'];
        
        $salt = $this->getCharAndNumr(6);
        $edsPassword = md5(md5($password) . $salt);
        $regip = $_SERVER["REMOTE_ADDR"];
        $memberUserModel = D('MemberUser');
        // 判断是否添加成功
        $addFlag = $memberUserModel->addUser($user_name, $real_name, $edsPassword, $group_id, $cellphone, $avatar, $salt, $regip);
        if ($addFlag >= 0)
        {
            $logArray[$this->operationAdd][] = "【账号管理】->【添加账号】";
            $this->addLog($logArray);
            header("Location: ?c=accountmanage&a=index");
        }
    }
    
    /**
     * 修改用户
     * 2016-9-14 14:21:12
     * @version 1.0
     */
    public function modify()
    {
        $id = $_REQUEST['id'];
        $userArray = D('MemberUser')->getUserById($id);
        $userInfo = $userArray[0];
        if ($userInfo['avatar'] == null)
        {
            $userInfo['avatar'] = C('URL_POST') . 'Public/img/slogo.jpg';
        }
        else 
        {
            $userInfo['avatar'] = C('URL_ADMIN_POST') . $userInfo['avatar'];
        }
        // 获取用户类型
        $groupArray = D('GroupInfo')->getGroupInfo();
        $this->assign('groupArray', $groupArray);
        $this->assign('userInfo', $userInfo);
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-14 14:21:41
     * sz
     */
    public function submit_modify ()
    {
        $user_id = $_POST['user_id'];
        $real_name = $_POST['real_name'];
        $password = $_POST['password'];
        $group_id = $_POST['group'];
        $cellphone = $_POST['cellphone'];
    
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array(
                'jpg',
                'gif',
                'png',
                'jpeg'
        ); // 设置附件上传类型
        $upload->rootPath = C('URL_ADMIN');// 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->uploadOne($_FILES['logo_head']);
        $avatar = $info['savepath'] . $info['savename'];
    
        if($password != null)
        {
            $salt = $this->getCharAndNumr(6);
            $edsPassword = md5(md5($password) . $salt);
        }
    
        $memberUserModel = D('MemberUser');
        // 判断是否添加成功
        $addFlag = $memberUserModel->modifyUser($user_id, $real_name, $edsPassword, $group_id, $cellphone, $avatar, $salt);
        if ($addFlag >= 0)
        {
            $logArray[$this->operationMod][] = "【账号管理】->【修改账号】";
            $this->addLog($logArray);
            header("Location: ?c=accountmanage&a=index");
        }
    }
    
    /**
     * 检测用户名是否存在
     * 2016-9-19 15:25:22
     * @version 1.0
     */
    public function check_user_name ()
    {
        $user_name = $_POST['user_name'];
        
        $memberUserModel = D('MemberUser');
        $selectFlag = $memberUserModel->checkAllMember($user_name);
        if ($selectFlag == null)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    
    /**
     * 更改状态
     * 2016-9-14 08:59:54
     * @version 1.0
     */
    public function update_status ()
    {
        $id = $_POST['id'];
        $status = $_POST['status'];
        // 判断是否更新成功
        if ($status == 1)
        {
            $status = 0;
        }
        elseif ($status == 0)
        {
            $status = 1;
        }
        $memberUserModel = D('MemberUser');
        $updateFlag = $memberUserModel->updateStatus($id, $status);
        if ($updateFlag >= 0)
        {
            $logArray[$this->operationModify][] = "【账号管理】->【更改状态】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    
    /**
     * 删除
     * 2016-9-14 09:17:25
     * sz
     */
    public function del ()
    {
        $id = $_POST['id'];
        $memberUserModel = D('MemberUser');
        // 判断是否删除成功
        $delFlag = $memberUserModel->del($id);
        if ($delFlag >= 0)
        {
            $logArray[$this->operationDelete][] = "【账号管理】->【删除账号】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
}